Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datadog: sample_rate omitted by default #1

Closed
wants to merge 3 commits into from

Conversation

dgoffredo
Copy link
Owner

@dgoffredo dgoffredo commented Jun 27, 2023

Here's what I plan to propose in a PR upstream.

It uses the magic float -1.0 for sample_rate to mean "use priority sampling instead," and then changes the default value to be -1.0.

It represents DatadogSampleRate as a *float32 instead of float32. This way, the default value of nil can be interpreted to mean "no rate specified." The configuration is decoded by the mapstructure package, which supports this style.

In order to conditionally include "sample_rate": ... in /etc/nginx/opentracing.json, I had to generalize the code that produces that file. I put the template-based implementation in a dedicated function, and that function is called for all tracers except Datadog. Datadog has its own function that uses the encoding/json package instead of text/template.

// Default: 1.0
DatadogSampleRate float32 `json:"datadog-sample-rate"`
// Default: use a dynamic rate instead
DatadogSampleRate *float32 `json:"datadog-sample-rate",omitempty`
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is configuration ever printed/serialized after being parsed into this struct? If so, non-nil DatadogSampleRate would probably render as the pointer value, which is not what we want.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't look like it.

Copy link

@cgilmour cgilmour left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some thoughts and suggestions

@@ -1084,37 +1074,65 @@ ratio = {{ .OtelSamplerRatio }}
parent_based = {{ .OtelSamplerParentBased }}
`

func datadogOpentracingCfg(cfg ngx_config.Configuration) (string, error) {
jsn := map[string]interface{}{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably just call this m instead of jsn, especially since it's not json here, even if it becomes it later.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

jsn["sample_rate"] = *cfg.DatadogSampleRate
}

jsnBytes, err := json.Marshal(jsn)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

b or buf instead of jsnBytes. The name barely carries meaning because it's a forced placeholder with a 5 line lifecycle

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

func createOpentracingCfg(cfg ngx_config.Configuration) error {
var tmpl *template.Template
var fileContent string

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its final purpose is to be the contents of a file, but at this stage it's conf, config or at a stretch, configData
The part that makes it fileContent is the os.WriteFile at the end, not what it is until that point.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

@@ -1001,8 +994,7 @@ func NewDefault() Configuration {
DatadogEnvironment: "prod",
DatadogCollectorPort: 8126,
DatadogOperationNameOverride: "nginx.handle",
DatadogSampleRate: 1.0,
DatadogPrioritySampling: true,
DatadogSampleRate: nil,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be needed, except for consistency

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's here for consistency.

} else {
tmpl, _ = template.New("empty").Parse("{}")
fileContent = "{}"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to just return here and skip creating a file, or log some warning? That might be handled somewhere else

Copy link
Owner Author

@dgoffredo dgoffredo Jun 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, I'd prefer to leave it as it was. The current behavior is to produce a file containing {}, you can even see it on disk in the container.

@dgoffredo
Copy link
Owner Author

Thanks for reviewing, @cgilmour. I'll type up a description for a PR to propose upstream.

@dgoffredo
Copy link
Owner Author

kubernetes#10151 proposed upstream.

@dgoffredo dgoffredo closed this Jun 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants